﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeSeHus.Import.Controller;
using HeSeHus.Import.Interface;
using HeSeHus.Model;
using HeSeHus.Interface;
using System.Windows.Media;
using System.Reflection;

namespace HeSeHus.Controller
{
    public class SystemController
    {

        ImportController _ic;
        Dictionary<IProduct, List<IProductDetail>> _importDic;
        Dictionary<Product, PropertyCollection> _returnDictionary;
        List<Property> _propertyCheckListe;
        int _propId;

        public SystemController()
        {
            _returnDictionary = new Dictionary<Product, PropertyCollection>();
        }

        public Dictionary<IProduct, List<IProductDetail>> ImportProduct(string sti)
        {
            _ic = new ImportController();
            Dictionary<IProduct, List<IProductDetail>> modtager = new Dictionary<IProduct, List<IProductDetail>>();

           return modtager = _ic.ExportProduct(sti);
        }

        public List<ICategory> ImportCategory(string sti)
        {
            _ic = new ImportController();
            

            return _ic.ExportCategory(sti);
        }

        public List<IBrand> ImportBrand(string sti)
        {
            _ic = new ImportController();
            

            return _ic.ExportBrand(sti);
        }

        public void DBCaller (Dictionary<IMProduct, List<IMProperty>> propertyDic, string connectionString, bool isNative)
        {
                DatabaseController dbc = new DatabaseController(isNative, connectionString);
                
                dbc.PopulateProducts(_importDic);
                dbc.PopulateProperties(propertyDic);
                dbc.PopulateProperties_Products(propertyDic);
        }

        public void DBCallerClean(string connectionString, bool isNative)
        {
                DatabaseController dbc = new DatabaseController(isNative, connectionString);
                dbc.CleanDatabase();
        }


        public void  ParseXML(string[] fileArray)
        {
            
            _importDic = new Dictionary<IProduct, List<IProductDetail>>();
            List<IBrand> brandImportListe = null;
            List<ICategory> categoryImportListe = null;

            for (int i = 0; i < fileArray.Length; i++)
            {
                if (fileArray[i].Contains("products"))
                {
                    //@Torben
                    //Kan nu modtage adskillige Product filer og "smelte dem sammen".
                    _importDic = _importDic.Union(ImportProduct("ConfigFiles/"+fileArray[i])).ToDictionary(a => a.Key, b => b.Value);
                }
                else if (fileArray[i].Contains("brands"))
                {
                    brandImportListe = ImportBrand("ConfigFiles/" + fileArray[i]);
                }
                else if (fileArray[i].Contains("categories"))
                {
                    categoryImportListe = ImportCategory("ConfigFiles/" + fileArray[i]);
                }
            }

            _propId = 1;
            PropertyCollection propertyColl = null;
            _propertyCheckListe = new List<Property>();

            //Det tog mig et pænt stykke tid at fatte, hvorfor returnDictionary blev ved at være tom....

            //Dictionary<Product, PropertyCollection> returnDictionary = new Dictionary<Product, PropertyCollection>();

            Product product = null;
            Property property = null;

            for (int productIndex = 0; productIndex < _importDic.Count; productIndex++)
            {
                propertyColl = new PropertyCollection();
                product = new Product(int.Parse(_importDic.Keys.ElementAt(productIndex).ProductNumber));

                #region CategoryParsing
                for (int categoryIndex = 0; categoryIndex < categoryImportListe.Count; categoryIndex++)
                {
                    if (int.Parse(_importDic.Keys.ElementAt(productIndex).Category) == categoryImportListe[categoryIndex].Id)
                    {
                        if (!_propertyCheckListe.Exists(x => x.Name == categoryImportListe[categoryIndex].Name))
                        {
                            property = new Property(categoryImportListe[categoryIndex].Name, _propId, categoryImportListe[categoryIndex].Description);
                            _propertyCheckListe.Add(property);
                            propertyColl.Add(property);
                            _propId++;
                        }
                        else
                        {
                            property = new Property(categoryImportListe[categoryIndex].Name, _propertyCheckListe.Find(x => x.Name == categoryImportListe[categoryIndex].Name).PropertiesId, categoryImportListe[categoryIndex].Description);
                            propertyColl.Add(property);
                        }
                    }

                } 
                #endregion

                #region BrandParsing
                for (int brandsIndex = 0; brandsIndex < brandImportListe.Count; brandsIndex++)
                {
                    if (_importDic.Keys.ElementAt(productIndex).BrandCode == brandImportListe[brandsIndex].BrandCode)
                    {
                        if (_propertyCheckListe.Exists(x => x.Value == brandImportListe[brandsIndex].BrandName))
                        {
                            property = new Property("Mærke", _propId, brandImportListe[brandsIndex].BrandName);
                            _propertyCheckListe.Add(property);
                            propertyColl.Add(property);
                            
                        }
                        else
                        {
                            property = new Property("Mærke", _propId, brandImportListe[brandsIndex].BrandName);
                            propertyColl.Add(property);
                        }
                    }

                } 
                
#endregion

                #region "Generisk" Importer
                /*Type[] arguments = importDic.Keys.GetType().GetGenericArguments();
                Type keyType = arguments[0];



                List<PropertyInfo> properties = new List<PropertyInfo>(keyType.GetProperties());



                for (int productPropertyIndex = 0; productPropertyIndex < importDic.Count; productPropertyIndex++)
                {
                    HeSeHus.Import.Model.Product pp = new Import.Model.Product(importDic.Keys.ElementAt(productPropertyIndex).ProductNumber, importDic.Keys.ElementAt(productPropertyIndex).BrandCode, importDic.Keys.ElementAt(productPropertyIndex).AvailabilityLevel, importDic.Keys.ElementAt(productPropertyIndex).Category, importDic.Keys.ElementAt(productPropertyIndex).ProductStatus, importDic.Keys.ElementAt(productPropertyIndex).LaunchDate);
                    foreach (var item in properties)
                    {
                        if (item.Name != "ProductNumber")
                        {

                            if (propertyCheckListe.Exists(x => x.Name == item.Name))
                            {
                                propId++;
                                property = new Property(item.Name, propId, item.GetValue(pp, null).ToString());
                                propertyCheckListe.Add(property);
                                propertyColl.Add(property);
                            }
                            else
                            {
                                property = new Property(item.Name, propId, item.GetValue(pp, null).ToString());
                                propertyColl.Add(property);
                            }
                        }
                    }

                } */
                #endregion

                _returnDictionary.Add(product, propertyColl); 
            }
           
           
        }
    
       
        public ImageSource[] PingDatabase(string[] ipAdresseArray, int port, bool[] isNative, string[] connectionStringArray)
        {
            ImageSource[] returnArray = new ImageSource[2];
            for (int i = 0; i < ipAdresseArray.Length; i++)
            {
                DatabaseController dbc = new DatabaseController();
                if (isNative[i])
                {
                    returnArray[0] = dbc.Ping(ipAdresseArray[0], 1433);
                }
                else
                {
                    returnArray[1] = dbc.Ping(ipAdresseArray[1], 1433);
                }

            }

            return returnArray;
        }

        public void TilføjProperty(string name, string value, IMProduct inObject)
        {
            foreach (var item in _returnDictionary.Keys)
            {
                if (item.ProdId == inObject.ProdId)
                {
                    if (!_propertyCheckListe.Exists(x=> x.Name == name))
                    {
                        _propId++;
                        Property property = new Property(name, _propId, value);
                        _propertyCheckListe.Add(property);
                        _returnDictionary[item].Add(property);
                    }
                    else
                    {
                        Property property = new Property(name, _propertyCheckListe.Find(x=> x.Name == name).PropertiesId, value);
                        _returnDictionary[item].Add(property);
                    }
                }
                
            }
        }

        //returnerer Et Interface Dictionary 
        public Dictionary<IMProduct, List<IMProperty>> ReturnDictionary
        {
            get
            {
                Dictionary<IMProduct, List<IMProperty>> tempproduct = new Dictionary<IMProduct, List<IMProperty>>();
                List<IMProperty> tempproperty;

                foreach (var prod in _returnDictionary)
                {
                    tempproperty = new List<IMProperty>();
                    foreach (var prop in prod.Value)
                    {
                        
                        tempproperty.Add((IMProperty)prop);
                    }

                    tempproduct.Add((IMProduct)prod.Key, tempproperty);
                }
                return tempproduct;
            }
        }
    }
}
